<template>
  <div class="login-user">
      <el-form ref="form" :model="form">
        <i class="el-icon-s-promotion" style="font-size:50px;text-align: center;width:100%"></i>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading='isloding' type="primary" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from '@/services/login'

export default {
  name: 'loginUser',
  data () {
    return {
      form: {
        phone: '18201288771',
        password: '111111'
      },
      isloding: false,
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '必须是手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '位数必须是6~18位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async onSubmit () {
      this.isloding = true
      try {
        await this.$refs.form.validate()

        const { data } = await login(this.form)
        if (data.state === 1) {
          this.$store.commit('setToken', data.content)
          this.$router.push(this.$route.query.redirct || '/')
          this.$message.success('登陆成功')
        } else {
          this.$message.error(data.message)
        }
      } catch (err) {
        this.$message.error('信息填写有误')
      }
      this.isloding = false
    }
  }
}
</script>

<style lang="scss" scoped>
.login-user {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;

  .el-form {
    width: 300px;
    padding: 20px;
    background-color: #fff;
    border-radius: 10px;

    .el-button {
      width: 100%;
      text-align: center;
    }
  }
}
</style>
